package com.taxigang.client.uibinder;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.client.uibinder.TaxiGangPresenter.CreateJoinMode;
import com.taxigang.client.uibinder.base.CreateStep;
import com.taxigang.shared.ChitInfo;
import com.taxigang.shared.LoginInfo;
import com.taxigang.shared.TaxiGangInfo;

public class Mobilenumber extends CreateStep {

	private static MobilenumberUiBinder uiBinder = GWT
			.create(MobilenumberUiBinder.class);

	interface MobilenumberUiBinder extends UiBinder<Widget, Mobilenumber> {
	}

	public Mobilenumber() {
		initWidget(uiBinder.createAndBindUi(this));
		bindUI();
		enterMobileNumberLabel.setText(TaxiGangCreator.taxiGangConstants.enterMobilenumber());
		okButton.setText(TaxiGangCreator.taxiGangConstants.cont());
		headerLabel.setText(TaxiGangCreator.taxiGangConstants.mobileNumber());
	}

	@UiField Button backButton;
	
	@UiField Button okButton;
	@UiField Label enterMobileNumberLabel;
	@UiField TextBox mobileNumberTextBox;
	@UiField
	Label headerLabel;

	private void bindUI() {
		backButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				PanelType panelType = PanelType.SEARCH;
				if (Session.getInstance().getCreateJoin()==CreateJoinMode.JOIN){
					panelType = PanelType.SUGGESTION;
				}
				taxiGangRoute.show(panelType);				

			}
		});
		
		okButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Session session = Session.getInstance();
				LoginInfo loginInfo = session.getLoginInfo();
				loginInfo.setMobilenumber(mobileNumberTextBox.getText());
				
				if (session.getCreateJoin()==CreateJoinMode.JOIN){
					TaxiGangInfo[] infoList = session.getTaxiGangSearchResultList();
					TaxiGangInfo taxiganginfo = infoList[session.getCurrentSuggestionIndex()];
					session.setJoinTaxiGangInfo(taxiganginfo);
					String message = TaxiGangCreator.taxiGangConstants.iJustJoined();
					ChitInfo chitInfo = new ChitInfo(message, new Date(), loginInfo.getNickname(), loginInfo.getMobilenumber());
					TaxiGangPresenter.getInstance().joinWithWait(chitInfo);
				} else {
					taxiGangRoute.show(PanelType.CREATE_STEP1);								
				}
			}
		});
	}

}
